package com.treasure.mall.biz.service.core.contact.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.entity.contact.ChatPO;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.repository.contact.ChatRepository;
import com.treasure.mall.biz.service.core.contact.ChatService;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.zbq.common.ienum.chat.ChatEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.contact.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/11 5:55 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Service
public class ChatServiceImpl extends AbstractSearchableCrudService<ChatPO, Long> implements ChatService {

    private ChatRepository chatRepository;
    @Autowired
    private MerchantService merchantService;

    protected ChatServiceImpl(ChatRepository repository) {
        super(repository);
        chatRepository = repository;
    }

    @Override
    protected SpecificationAndSort<ChatPO> specificationAndSort(BaseSearchDTO searchDTO) {
        return null;
    }

    /**
     * 根据商家ID和用户ID，筛选出聊天列表
     *
     * @param merchantId
     * @return
     */
    @Override
    public List<ChatPO> findAll(Long merchantId) {
        return chatRepository.findByMerchantIdAndDeletedIsFalse(merchantId);
    }

    @Override
    public List<ChatPO> findAllByUserId(Long userId) {
        return chatRepository.findByUserPO_UserIdAndDeletedIsFalseOrderByLastReplyTimeDesc(userId);
    }

    /**
     * 建立聊天分组
     *
     * @param user
     * @param merchantId
     * @param groupId
     * @param groupName
     */
    @Override
    public void createGroup(UserPO user, Long merchantId, String groupId, String groupName) {
        MerchantPO merchantPO = merchantService.findById(merchantId);
        ChatPO po = new ChatPO();
        po.setGroupId(groupId);
        po.setMerchantId(merchantId);
        po.setUserPO(user);
        po.setCreateTime(LocalDateTime.now());
        po.setGroupName(groupName);
        po.setNoReadCount(0L);
        po.setUserNoReadCount(0L);
        po.setChatType(ChatEnums.ChatType.contact);
        po.setLastReplyTime(LocalDateTime.now());
        if (merchantPO != null) {
            po.setLogo(merchantPO.getData().getAvatar());
        }
        save(po);
    }

    /**
     * 恢复聊天回话
     *
     * @param userId
     * @param merchantId
     */
    @Override
    public void enableGroup(Long userId, Long merchantId) {
        chatRepository.enableChat(userId, merchantId);
    }

    /**
     * 删除聊天回话
     *
     * @param id
     */
    @Override
    public void deleteGroup(Long id) {
        chatRepository.deleteChat(id);
    }

    /**
     * 重置未读数量
     *
     * @param userId
     * @param merchantId
     */
    @Override
    public void resetNoReadCount(Long userId, Long merchantId) {
        chatRepository.resetNoReadCount(userId, merchantId);
    }

    /**
     * 重置用户未读数量
     *
     * @param userId
     * @param merchantId
     */
    @Override
    public void resetUserNoReadCount(Long userId, Long merchantId) {
        chatRepository.resetUserNoReadCount(userId, merchantId);
    }

    /**
     * 修改回话最后回复消息
     *
     * @param userId     用户ID
     * @param merchantId 商户ID
     * @param message    最后回复的消息
     */
    @Override
    public void modifyLastMessage(Long userId, Long merchantId, String message) {
        chatRepository.modifyLastMessage(userId, merchantId, message, LocalDateTime.now());
    }

    @Override
    public void modifyLastMessageByMerchantId(Long userId, Long merchantId, String message) {
        chatRepository.modifyLastMessageByMerchantId(userId, merchantId, message, LocalDateTime.now());
    }

    /***
     * 判断群组是否存在
     * @param userId
     * @param merchantId
     * @return
     */
    @Override
    public boolean exist(Long userId, Long merchantId) {
        return chatRepository.countByUserPO_UserIdAndMerchantIdAndDeletedIsFalse(userId, merchantId) > 0;
    }

    @Override
    public ChatPO save(BaseSaveDTO saveDTO) {
        return null;
    }


}
